﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.Mvc;
using Dogshow.Model;
using Dogshow.Model.Validation;
using Dogshow.Web.Services;
using Dogshow.Web.ViewModels.Izlozbe;
using DogshowRepository;
using Ninject;

namespace Dogshow.Web.Controllers
{
    public class IzlozbeController : Controller
    {
        private const int IdOrganizatora = 1;

        private IRepoServices m_RepoService;

        public IzlozbeController(IRepoServices mRepoService)
        {
            m_RepoService = mRepoService;
        }

        public ActionResult Unos()
        {
            var izlozba = m_RepoService.VratiIzlozbu(1) ?? new Izlozba() { IdOrganizatora = IdOrganizatora, Organizator = new KinoloskoDrustvo { Naziv = "KINOLOŠKI SAVEZ REPUBLIKE SRBIJE", Mesto = new Mesto { Naziv = "BEOGRAD" } } }; ;
            DodajPodesavanjaZaTituleChekcBoxList(izlozba.Id);
            return View("", izlozba);
        }

        [HttpPost]
        public ActionResult SacuvajOsnovnePodatkeOIzlozbi(Izlozba izlozba)
        {
            if (ModelState.IsValid)
            {
                var id = m_RepoService.SacuvajPodatkeOIzlozbi(izlozba);
                var titule = CheckBoxListExtension.GetSelectedValues<int>("cblTitule");
                m_RepoService.SacuvajTituleIzlozbe(id, titule);
                return RedirectToAction("Unos");
            }
            return View("Unos", izlozba);
        }

        public ActionResult SudijeGridViewPartial(int IdIzlozbe)
        {
            var raspored = ((IKernel) Bootstrap.Bootstrapper.Container).Get<IIzlozbeRepository>().VratiSudijePoRingovima(IdIzlozbe);
            var model = new SudijeViewModel {IdIzlozbe = IdIzlozbe, SudijePoRingovima = raspored};
            return PartialView("_SudijeGridView", model);
        }

        public PartialViewResult UnosSudijeComboBoxCallback()
        {
            return PartialView("_UnosSudijeComboBox");
        }

        public PartialViewResult DodajSudijuURingu(int IdIzlozbe, SudijaURingu sudija)
        {
            var izlozbeRepository = ((IKernel)Bootstrap.Bootstrapper.Container).Get<IIzlozbeRepository>();
            if (ModelState.IsValid)
            {
                izlozbeRepository.DodajSudijuURingu(sudija);
            }
            var raspored = izlozbeRepository.VratiSudijePoRingovima(IdIzlozbe);
            var model = new SudijeViewModel { IdIzlozbe = IdIzlozbe, SudijePoRingovima = raspored };
            return PartialView("_SudijeGridView", model);
        }

        public PartialViewResult AzurirajSudijuURingu(SudijaURingu sudija)
        {
            var izlozbeRepository = ((IKernel)Bootstrap.Bootstrapper.Container).Get<IIzlozbeRepository>();
            if (ModelState.IsValid)
            {
                izlozbeRepository.SacuvajSudijuURingu(sudija);    
            }
            var raspored = izlozbeRepository.VratiSudijePoRingovima(sudija.IdIzlozbe);
            var model = new SudijeViewModel { IdIzlozbe = sudija.IdIzlozbe, SudijePoRingovima = raspored };
            return PartialView("_SudijeGridView", model);
        }

        public PartialViewResult ObrisiSudijuIRing(SudijaURingu sudijaURingu)
        {
            var izlozbeRepository = ((IKernel)Bootstrap.Bootstrapper.Container).Get<IIzlozbeRepository>();
            izlozbeRepository.ObrisiSudijuURingu(sudijaURingu);
            var raspored = izlozbeRepository.VratiSudijePoRingovima(sudijaURingu.IdIzlozbe);
            var model = new SudijeViewModel { IdIzlozbe = sudijaURingu.IdIzlozbe, SudijePoRingovima = raspored };
            return PartialView("_SudijeGridView", model);
        }

        public PartialViewResult RaseURinguGridViewCallback(int idIzlozbe, int idSudije)
        {
            var model = new RasePoRingovimaViewModel {IdIzlozbe = idIzlozbe, IdSudije = idSudije};
            var izlozbeRepository = ((IKernel)Bootstrap.Bootstrapper.Container).Get<IIzlozbeRepository>();
            var rase = izlozbeRepository.VratiRasePasaZaSudiju(idIzlozbe, idSudije);
            model.RaseURingu = rase;
            return PartialView("_RaseURinguGridView", model);
        }

        public PartialViewResult DodajRasuURing(int IdIzlozbe, int IdSudije, RasaURingu rasa)
        {
            rasa.IdIzlozbe = IdIzlozbe;
            rasa.IdSudije = IdSudije;
            var izlozbeRepository = ((IKernel)Bootstrap.Bootstrapper.Container).Get<IIzlozbeRepository>();
            if (izlozbeRepository.PostojiRasaURingu(rasa))
            {
                throw new InvalidOperationException("Uneta rasa već postoji");
            }
            izlozbeRepository.DodajRasuSudiji(rasa);
            var model = new RasePoRingovimaViewModel
            {
                IdIzlozbe = IdIzlozbe,
                IdSudije = IdSudije,
                RaseURingu = izlozbeRepository.VratiRasePasaZaSudiju(IdIzlozbe, IdSudije)
            };
            return PartialView("_RaseURinguGridView", model);
            
        }

        public PartialViewResult ObrisiRasuIzRinga(RasaURingu rasa)
        {
            var izlozbeRepository = ((IKernel)Bootstrap.Bootstrapper.Container).Get<IIzlozbeRepository>();
            izlozbeRepository.ObrisiRasuOdSudije(rasa);
            var model = new RasePoRingovimaViewModel
            {
                IdIzlozbe = rasa.IdIzlozbe,
                IdSudije = rasa.IdSudije,
                RaseURingu = izlozbeRepository.VratiRasePasaZaSudiju(rasa.IdIzlozbe, rasa.IdSudije)
            };
            return PartialView("_RaseURinguGridView", model);
        }

        private void DodajPodesavanjaZaTituleChekcBoxList(int idIzlozbe)
        {
            var titule = m_RepoService.VratiMoguceTitule();
            var tituleIzlozbe = m_RepoService.VratiTituleIzlozbe(idIzlozbe);
            var cbs = new CheckBoxListSettings()
            {
                Name = "cblTitule",
            };
            cbs.ControlStyle.CssClass = "editor";
            cbs.Properties.TextField = "Naziv";
            cbs.Properties.ValueType = typeof(int);
            cbs.Properties.ValueField = "Id";
            cbs.Properties.RepeatDirection = RepeatDirection.Vertical;
            var tituleItems = titule.Select(x => new ListEditItem { Value = x.Id, Text = x.Naziv }).ToList();
            cbs.Properties.Items.AddRange(tituleItems);
            foreach (ListEditItem item in cbs.Properties.Items)
            {
                item.Selected = tituleIzlozbe.Any(x => x.Id == (int)item.Value);
            }

            ViewBag.CheckBoxSettings = cbs;
        }
    }
}
